
var Toolbar = new Class ({

	options: {
		toolbar:  false,
		fontbar:  false,
		emoticon: false
	},
	
	initialize: function(id, options){
		this.winid = id;
        this.setOptions (options);
    },
    
   getId: function () {
   		return this.winid;
   },
   
   setOptions: function (props) {
   		this.options = props;
   	},
   
   buildHTMLToolbar: function (userid) {
   		var toolbar = '';
   		toolbar = '<div id="'+toolbarmain+'">\n'+
   				  '<ul class="nav">\n'+
   				  this.buildHTMLButtons(userid) +
				  this.buildHTMLFontbar(userid) +
				  this.buildHTMLEmoticons(userid) +
				  '</ul>';
				  '</div>\n';  
		return toolbar;
	},
	
	buildHTMLButtons: function (userid) {
		var buttons = '';
		if (this.options.toolbar == true) {
   			buttons = '<li><div class="imToolbar" onselectstart="return false;" onmousemove="return false;">\n'+
				  	  '<img id="'+this.getId()+'_bold" alt="Bold" ' + this.buildHTMLButtonActions(userid, 'Bold')+ ' src="'+pathTous+'themes/default/window/bold_off.png"/>\n'+
				  	  '<img id="'+this.getId()+'_italic" alt="Italic" ' + this.buildHTMLButtonActions(userid, 'Italic')+ ' src="'+pathTous+'themes/default/window/italic_off.png"/>\n'+
				  	  '<img id="'+this.getId()+'_underline" alt="Underline" ' + this.buildHTMLButtonActions(userid, 'Underline')+ ' src="'+pathTous+'themes/default/window/underline_off.png"/>\n'+
				  	  '</div></li>\n';
		}
		return buttons;
	},	
	
	buildHTMLButtonActions: function (userid, type) {
		var action = '';
		action = 'onmousedown="return false;" '+
				 'onclick="imManager.windows[\''+userid+'\'].getToolbar().toggle'+type+'();" '+
				 'onmouseout="buttonNormal(this);" '+
				 'onmouseover="buttonHover(this);"';
				 
		return action;
	},
	
	buildHTMLFontbar: function (userid) {
		var fontbar = '';
		if (this.options.fontbar == true) {
			fontbar = '<li id="'+this.getId()+fontListSuffix+'" class="fontlist">'+
					  '<a id="'+this.getId()+setFontSuffix+'" class="setFontLink" href="#" onselectstart="return false;" onclick="imManager.windows[\''+ userid + '\'].getToolbar().toggleFontList(\''+userid+'\');return false;">Tahoma</a>'+
					  this.getFontList(userid) +
					  '</li>'+
					  '<li class="fslist">'+
				  	  '<a id="'+this.getId()+setFontSizeSuffix+'" class="setFontSizeLink" onselectstart="return false;" onclick="imManager.windows[\''+ userid + '\'].getToolbar().toggleFontSizeList(\''+userid+'\');return false;" href="#">12</a>'+
				  	  this.getFontSizeList(userid) +
				  	  '</li>'+
				  	  '<li>'+
				  	  '<a id="'+this.getId()+setFontColorSuffix+'" class="setFontColorLink" onselectstart="return false;" onclick="imManager.windows[\''+ userid + '\'].getToolbar().toggleFontColorList(\''+userid+'\');return false;" href="#">'+
				     	'<span id="'+this.getId()+setFontColorColor+'" style="width: 14px; height: 14px; display: block; background-color: rgb(0, 0, 0);"/>'+
				      '</a>\n'+
				      this.getFontColorList(userid)+
				      '</li>';
		}
		return fontbar;
	},
	
	buildHTMLEmoticons: function (userid) {
		var emoti = '';
		if (this.options.emoticon == true) {
			emoti = '<li>'+
					'<a class="insertEmoticonLink" onselectstart="return false;" onclick="imManager.windows[\''+ userid + '\'].getToolbar().toggleEmoticonList();return false;" href="#">'+
					'<img width="14" height="14" style="border: 0pt none ;" src="'+pathTous+'themes/default/emoticons/mini_smile.gif"/>'+
					'</a>\n'+
					this.getEmoticonsList(userid);
					'</li>';
		}
		return emoti;
	},
	
	/**
    * @author Joshua Gross
    **/
   toggleBold: function() {
      var winId = this.getId();
      var sendBox = $(winId + inputSuffix);
         
      sendBox.setOpacity(0); // horrah weird Opera 9 input refresh!-
      if(sendBox.style.fontWeight == '400' || sendBox.style.fontWeight == '') {
         $(winId + '_bold').src = pathTous + 'themes/' + theme + '/window/bold_on.png';
         sendBox.setStyles({fontWeight: '700'});
      } else {
         sendBox.setStyles({fontWeight: '400'});
         $(winId + '_bold').src = pathTous + 'themes/' + theme + '/window/bold_off.png';
      }
      sendBox.setOpacity(1); // horrah weird Opera 9 input refresh!
      //setTimeout("$('" + winId + "_sendBox').focus();", 125);
   },

   /**
    * @author Joshua Gross
    **/
   toggleItalic: function() {
      var winId = this.getId();
      var sendBox = $(winId + inputSuffix);
      
      sendBox.setOpacity(0); // horrah weird Opera 9 input refresh!
      if(sendBox.style.fontStyle == 'normal' || sendBox.style.fontStyle == '') {
         sendBox.setStyles({fontStyle: 'italic'});
         $(winId + '_italic').src = pathTous + 'themes/' + theme + '/window/italic_on.png';
      } else {
         sendBox.setStyles({fontStyle: 'normal'});
         $(winId + '_italic').src = pathTous + 'themes/' + theme + '/window/italic_off.png';
      }
      sendBox.setOpacity(1); // horrah weird Opera 9 input refresh!
      //setTimeout("$('" + winId + "_sendBox').focus();", 125);
   },

   /**
    * @author Joshua Gross
    **/
   toggleUnderline: function() {
      var winId = this.getId();
      var sendBox = $(winId + inputSuffix);
      
      sendBox.setOpacity(0); // horrah weird Opera 9 input refresh!
      if(sendBox.style.textDecoration == 'none' || sendBox.style.textDecoration == '') {
         sendBox.setStyles({textDecoration: 'underline'});
         $(winId + '_underline').src = pathTous + 'themes/' + theme + '/window/underline_on.png';
      } else {
         sendBox.setStyles({textDecoration: 'none'});
         $(winId + '_underline').src = pathTous + 'themes/' + theme + '/window/underline_off.png';
      }
      sendBox.setOpacity(1); // horrah weird Opera 9 input refresh!
      //setTimeout("$('" + winId + "_sendBox').focus();", 125);
   },
    
    /**
    * @author g henson
    **/
   toggleFontList: function(userid) {
            
      //$('emoticonList', 'fontColorList', 'fontSizeList').invoke('hide');
      
      // search of the font family list within the document
      var fL = $(toolbarmain).getElementsBySelector('.nav li ul').filterById(this.getId()+"_ff");
      // if the list is visible
      if (fL.getStyle('left') == 'auto') {
        // we're hidding it
      	fL.setStyle('left', '-999em');
      }
      // if it's not visible
      else {
        // we're displaying it
      	fL.setStyle('left', 'auto');
      }
   },
   
   /**
    * @author G. Henson
    **/
   toggleFontSizeList: function(userid) {
      // search of the font size list within the document
      var fsL = $(toolbarmain).getElementsBySelector('.nav li ul').filterById(this.getId()+"_fs");
      // if the list is visible
      if (fsL.getStyle('left') == 'auto') {
        // we're hidding it
      	fsL.setStyle('left', '-999em');
      }
      // if it's not visible
      else {
        // we're displaying it
      	fsL.setStyle('left', 'auto');
      }
   },

   /**
    * @author G. Henson
    **/
   toggleFontColorList: function(userid) {
      
      // search of the font size list within the document
      var fcL = $(toolbarmain).getElementsBySelector('.nav li ul').filterById(this.getId()+"_fc");
      // if the list is visible
      if (fcL.getStyle('left') == 'auto') {
        // we're hidding it
      	fcL.setStyle('left', '-999em');
      }
      // if it's not visible
      else {
        // we're displaying it
      	fcL.setStyle('left', 'auto');
      }
   },
   
   /**
    * @author G. Henson
    **/
   toggleEmoticonList: function() {
      // search of the font size list within the document
      var feL = $(toolbarmain).getElementsBySelector('.nav li ul').filterById(this.getId()+"_emo");
      // if the list is visible
      if (feL.getStyle('left') == 'auto') {
        // we're hidding it
      	feL.setStyle('left', '-999em');
      }
      // if it's not visible
      else {
        // we're displaying it
      	feL.setStyle('left', 'auto');
      }
   },

   /**
    * @author Joshua Gross
    * modified by G. Henson
    **/
   setFont: function(fontname) {
      var winId = this.getId();
      var sendBox = $(winId + inputSuffix);

      sendBox.setOpacity(0);
      sendBox.setStyles({fontFamily: fontname + ', sans-serif'});
      sendBox.setOpacity(1);
      
      $(winId + setFontSuffix).innerHTML = fontname;
      //setTimeout("$('" + winId + "_textarea').focus();", 125);
      this.toggleFontList('');
   },
   
   /**
    * @author Joshua Gross
    **/
   setFontSize: function(size) {
      var winId = this.getId();
      var sendBox = $(winId + inputSuffix);
      
      sendBox.setOpacity(0);
      sendBox.setStyles({fontSize: size + 'px'});
      sendBox.setOpacity(1);
      
      $(winId + setFontSizeSuffix).innerHTML = size;
      //setTimeout("$('" + winId + "_sendBox').focus();", 125);
      this.toggleFontSizeList('');
   },

   /**
    * @author Joshua Gross
    **/
   setFontColor: function(color) {
      var winId = this.getId();
      var sendBox = $(winId + inputSuffix);
      
      sendBox.setStyles({color: color});
      
      $(winId + setFontColorColor).setStyles({backgroundColor: color});
      //setTimeout("$('" + winId + "_sendBox').focus();", 125);
      this.toggleFontColorList('');
   },
   
   getFontList: function(userid) {
   		var fontlist = 	'<ul id="' + this.getId()+'_ff" class="fontlist">'+
   						'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Arial\');return false;">Arial</a></li>'+
   						'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Comic Sans MS\');return false;">Comic Sans MS</a></li>'+
   						'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Courier New\');return false;">Courier New</a></li>'+
					   	'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Garamond\');return false;">Garamond</a></li>'+
					   	'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Georgia\');return false;">Georgia</a></li>'+
					   	'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Impact\');return false;">Impact</a></li>'+
					   	'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Tahoma\');return false;">Tahoma</a></li>'+
					   	'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Times New Roman\');return false;">Times New Roman</a></li>'+
					   	'<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFont(\'Verdana\');return false;">Verdana</a></li>'+
					   	'</ul>';
		return fontlist;
	},
	
	getFontSizeList: function (userid) {
		var sizelist = '<ul id="' + this.getId()+'_fs" class="fslist">'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(8);return false;">8</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(10);return false;">10</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(12);return false;">12</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(14);return false;">14</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(16);return false;">16</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(18);return false;">18</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(20);return false;">20</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(22);return false;">22</a></li>'+
					   '<li><a href="#" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontSize(24);return false;">24</a></li>'+
					   '</ul>';
		return sizelist;
	},
	
	getFontColorList: function (userid) {
	
		var colorlist = '<ul id="' + this.getId()+'_fc" class="fclist">'+
						      '<li>'+
						      '<table><tr>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#000000;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#000000\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#b8b8b8;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#b8b8b8\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#b4ad3b;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#b4ad3b\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#bb5c54;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#bb5c54\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#755a5c;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#755a5c\');"></td>'+
						      '</tr></table>'+
						      '</li><li>'+
						      '<table><tr>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#a9b5ef;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#a9b5ef\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#d65a20;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#d65a20\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#e39230;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#e39230\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#a71334;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#a71334\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#590099;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#590099\');"></td>'+
						      '</tr></table>'+
						      '</li><li>'+
						      '<table><tr>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#d40088;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#d40088\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#0030ac;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#0030ac\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#676f11;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#676f11\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#769321;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#769321\');"></td>'+
						         '<td class="colorItem" style="width:13px;height:13px;border:1px solid #000;background-color:#3966fe;" onclick="imManager.windows[\''+userid+'\'].getToolbar().setFontColor(\'#3966fe\');"></td>'+
						      '</tr></table>'+
						      '</li>'+
						   '</ul>';
		return colorlist;
	},
	
	getEmoticonsList: function (userid) {
	
		var emoticons = '<ul id="' + this.getId()+'_emo">'+
						'<li>'+
					    '<table>'+
					      '<tr>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/smile.png" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':)\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/grin.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':D\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/wink.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\';)\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/embarassed.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':[\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/love.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':*\');return false;" /></a></td>'+
					      '</tr></table></li>'+
					      '<li>'+
					      '<table><tr>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/sad.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':(\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/cry.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':\\\'(\');" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/cool.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\'8-)\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/angry.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':angry:\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/surprised.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':O\');return false;" /></a></td>'+
					      '</tr></table></li>'+
					      '<li>'+
					      '<table><tr>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/serious.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':|\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/sick.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':-S\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/tongue.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':P\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/heart.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':heart:\');return false;" /></a></td>'+
					         '<td><a href="#" onmousedown="return false;"><img class="emotIcon" src="'+pathTous+'themes/default/emoticons/heartbroken.gif" width="19" height="19" style="border:0;" onclick="imManager.windows[\''+userid+'\'].insertText(\':brokenheart:\');return false;" /></a></td>'+
					      '</tr></table></li>'+
					      '</ul>';
		return emoticons;
	}
   
});